local cjson = require "cjson";


local cdp_key = ngx.var.cookie_cdp_key
local cdp_user = ngx.var.cookie_cdp_user

local url = ngx.req.get_uri_args().url
if not url then
	ngx.say('the url arg is not found!')
	return
end
if string.find(url, '?') then
	url = url .. '&'
else
	url = url .. '?'
end
local loginUrl = "/sso/login?cdp_key=".. cdp_key .."&cdp_user=" .. cdp_user
local res = ngx.location.capture(loginUrl)
if res.status == 200 then
	local resbody = cjson.decode(res.body)
	if resbody.code==0 then
		local token = resbody.data.token
		ngx.redirect(url .. 'token=' .. token)
	else
		ngx.say(cjson.encode(resbody))
	end
	
else
	ngx.say(cjson.encode(res))
end
